package com.zlgs.cqyf.vote;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URI;
import java.util.Date;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;

import microsoft.exchange.webservices.data.core.ExchangeService;
import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
import microsoft.exchange.webservices.data.core.enumeration.property.BodyType;
import microsoft.exchange.webservices.data.core.service.item.EmailMessage;
import microsoft.exchange.webservices.data.credential.ExchangeCredentials;
import microsoft.exchange.webservices.data.credential.WebCredentials;
import microsoft.exchange.webservices.data.property.complex.EmailAddress;
import microsoft.exchange.webservices.data.property.complex.MessageBody;


public class SendMailServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private  String emailAccount = "";
	private  String emailPassword = "";
	private  String emailSMTPHost = "";
	private  String receviMail = "";
	private  String smtpPort = "";

	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		this.emailAccount = config.getInitParameter("emailAccount");
		this.emailPassword = config.getInitParameter("emailPassword");
		this.emailSMTPHost = config.getInitParameter("emailSMTPHost");
		this.receviMail 	= config.getInitParameter("receviMail");
		this.smtpPort 	= config.getInitParameter("smtpPort");

	}

	public SendMailServlet() {
		super();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String msgContent = request.getParameter("msgContent");
		response.getWriter().append("this requst just support post request! " +msgContent);
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json; charset=utf-8");
		PrintWriter printWriter = response.getWriter();
		JSONObject retObject = new JSONObject();
		try {
		 
			try
	 		{
	 			ExchangeService service = getExchangeService("AE_CHINA_CQYFAS_OA <ae_china_cqyfas_oa@adient.com>","");
		        EmailMessage msg = new EmailMessage(service);
		        msg.setSubject("ces");
		        MessageBody body = MessageBody.getMessageBodyFromText("测试邮件发送");
		        body.setBodyType(BodyType.HTML);
		        msg.setBody(body);
		        EmailAddress fromAddress=new EmailAddress("ae_china_cqyfas_oa@adient.com");
		        msg.setFrom(fromAddress);
		        EmailAddress toEmailAddress=new EmailAddress("923379806@qq.com");
		        msg.getToRecipients().add(toEmailAddress);
		        msg.sendAndSaveCopy();
	 		}catch(Exception exception)
	 		{
	 			exception.printStackTrace();
	 			throw exception;
	 		}
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
			retObject.put("code", -1);
			retObject.put("msg", e.getMessage());
		}finally {
			printWriter.print(retObject.toString());
		} 
	}
	public static ExchangeService getExchangeService(String account, String password) throws Exception
	{
	    try
	    {
	      ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
	      ExchangeCredentials credentials = new WebCredentials(account, password, "autoexpr");
	      service.setCredentials(credentials);
	      service.setUrl(new URI("https://mail.adient.com/EWS/Exchange.asmx"));
	      return service;
	    } catch (Exception e) 
	    {
	       e.printStackTrace(); 
	       throw e;
	      }
	}
	 

}
